package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.utils.Timer;
	/**
	 * ѧϰһ��BitmapData��Ӧ��
	 */
	[SWF(width="400",height="300", backgroundColor="#ffffff",frameRate="30")]
	public class BitmapDataTest extends Sprite
	{
		private var dot:DotSample;
		private var stageW:int = 400;
		private var stageH:int = 300;
		private var timer:Timer = new Timer(30, 0);
		private var bf:BlurFilter;
		
		private var bmd:BitmapData;
		private var copyBmd:BitmapData;
		private var canvas:Bitmap;
		private var mtx:Matrix;
		private var tm:ColorTransform;
		private var trc:Matrix;
		public function BitmapDataTest()
		{
			init();
			putBg();
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
		}
		private function init():void {
			dot = new DotSample();
			
			bmd = new BitmapData(stageW, stageH, false, 0);
			copyBmd = new BitmapData(stageW, stageH, false, 0);
			canvas = new Bitmap(bmd);
			
			mtx = new Matrix();
			mtx.translate(stageW / 2, stageH / 2);
			trc = new Matrix();
			trc.translate(10, 10);
			
			bf = new BlurFilter(4, 4, 2);
			tm = new ColorTransform(0.9, 0.8, 0.8);
		}
		/**
		 * ���ñ���
		 */
		private function putBg():void {
			var bmd:BitmapData = new BitmapData(stageW, stageH, false, 0);
			var bm:Bitmap = new Bitmap(bmd);
			addChild(bm);
		}
		/**
		 * ��Ҫ��ʾ����
		 */
		private function putCanvas():Bitmap {
			copyBmd = bmd.clone();
			bmd.draw(copyBmd, trc, tm, null, null, true);
			bmd.draw(dot, mtx, null, null, null, true);
			bmd.applyFilter(bmd, bmd.rect, new Point, bf);
			canvas.x = mouseX-stageW/2;
			canvas.y = mouseY-stageH/2;
			return canvas;
		}
	
		private function onTimer(e:TimerEvent):void {
			var canvas:Bitmap = putCanvas();
			addChild(canvas);
		}
	}
	
}
